package user

import (
	"fiber-server/database"
	"fmt"

	"github.com/gofiber/fiber/v2"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	UserName string `json:"username"`
	Password string `json:"password"`
}

func GetUsers(c *fiber.Ctx) error {
	db := database.DBConn
	var users []User
	if err := db.Find(&users).Error; err != nil {
		return c.JSON(fiber.Map{
			"code": -1,
			"msg":  "get users faild",
		})
	}
	return c.JSON(users)
}

func GetUser(c *fiber.Ctx) error {
	id := c.Params("id")
	fmt.Println("id=", id)

	db := database.DBConn
	var user User
	if err := db.First(&user, id).Error; err != nil {
		return c.JSON(fiber.Map{
			"code": -1,
			"msg":  "get user faild",
			"data": fiber.Map{},
		})
	}

	return c.JSON(user)
}

func NewUser(c *fiber.Ctx) error {
	var user User
	c.BodyParser(&user)
	db := database.DBConn

	// 判断用户名是否存在
	username := user.UserName
	var userR User
	db.First(&userR, "user_name=?", username)
	fmt.Printf("%+v", userR)
	if userR.ID != 0 {
		return c.JSON(fiber.Map{
			"code": -1,
			"msg":  "user is exists",
		})
	}

	if err := db.Create(&user).Error; err != nil {
		return c.JSON(fiber.Map{
			"code": -1,
			"msg":  "add user faild",
		})
	}
	return c.JSON(user)
}

func UpdateUser(c *fiber.Ctx) error {
	var user User
	c.BodyParser(&user)
	id := c.Params("id")
	db := database.DBConn

	// 查询数据是否存在
	var userR User
	db.First(&userR, id)
	if userR.UserName == "" {
		return c.JSON(fiber.Map{
			"id":  id,
			"msg": "Username is not found by given ID",
		})
	}

	// 更新数据
	if err := db.Model(&user).Where("id", id).Update("password", user.Password).Error; err != nil {
		return c.JSON(fiber.Map{
			"code": -1,
			"msg":  "update user faild",
		})
	}

	// gorm update example:
	// db.Model(&product).Update("Price", 200)
	// db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // non-zero fields
	// db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

	userR.Password = user.Password

	return c.JSON(fiber.Map{
		"user": userR,
		"msg":  "Update successfully",
	})
}

func DeleteUser(c *fiber.Ctx) error {
	id := c.Params("id")
	db := database.DBConn
	var user User
	db.First(&user, id)
	if user.UserName == "" {
		return c.Status(500).SendString("No user found width given ID")
	}

	// 这里gorm只是逻辑删除
	db.Delete(&user)
	return c.JSON(fiber.Map{
		"user": user,
		"msg":  "Deletd success",
	})
}

func Login(c *fiber.Ctx) error {
	var user User
	c.BodyParser(&user)

	// 参数校验
	if user.UserName == "" || user.Password == "" {
		return c.JSON(fiber.Map{
			"code": -1,
			"msg":  "用户名或密码不能为空",
		})
	}

	db := database.DBConn
	// 判断用户名是否存在
	username := user.UserName
	var userR User
	db.First(&userR, "user_name=?", username)
	fmt.Printf("%+v", userR)

	if userR.UserName != user.UserName {
		return c.JSON(fiber.Map{
			"code": -1,
			"msg":  "用户名不存在",
		})
	}

	if userR.Password != user.Password {
		return c.JSON(fiber.Map{
			"code": -1,
			"msg":  "用户名或密码不正确",
		})
	}

	return c.JSON(fiber.Map{
		"code":     20000,
		"message":  "登录成功",
		"userInfo": user,
	})
}
